﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLogicLayer;
using BusinessEntities;

namespace QL_HoSoBA
{
    public partial class NhanVien : Form
    {
        NhanVien_Manager nv_manager = new NhanVien_Manager();
        NhanVien_Info nv_info = new NhanVien_Info();
    
        
        public NhanVien()
        {
            InitializeComponent();
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
          if( MessageBox.Show("Bạn Chắc Chứ?","Thông Báo",MessageBoxButtons.YesNo,MessageBoxIcon.Exclamation)==DialogResult.Yes)
            {
                Dispose();

            }
        }

        private void NhanVien_Load(object sender, EventArgs e)
        {
           
            this.LoadDanhSach();
       
            

        }
        private void LoadDanhSach()
        {

            this.dgvdanhsach.DataSource = nv_manager.NV_SelectALL();

           

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            int kq = 0;
                     kq=  nv_manager.NV_Insert(GanGiaTriChoNhanVien_Info());
                     try
                     {
                         if (kq != 0)
                         {
                             MessageBox.Show("Thêm Thành Công!!");
                          this.LoadDanhSach();
                           
                            
                         }
                         else
                             MessageBox.Show("Mã Nhập Vào bị trùng,Vui lòng nhập lại","Lỗi Nhập",MessageBoxButtons.OKCancel);
                     }
                     catch
                     {

                     }


        }
        private NhanVien_Info GanGiaTriChoNhanVien_Info()
        {
            nv_info.MaNV = mtxtMaNV.Text;
            nv_info.TenNV = txtHoTen.Text;
            nv_info.NgaySinh = dtpNgaySinh.Value;
            nv_info.DiaChi = txtDiaChi.Text;
            nv_info.SDT = txtsdt.Text;
            nv_info.Email = txtEmail.Text;
            nv_info.ChucVu = cbbChuvu.Text;
            nv_info.Phong = cbbPhongban.Text;
            return nv_info;
           
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            int kq = 0;
            kq = nv_manager.NV_Update(GanGiaTriChoNhanVien_Info());
            try
            {
                if (kq != 0)
                {
                    MessageBox.Show("Update Thành Công!!");
                    this.LoadDanhSach();
                    //NhanVien_Load(sender, e);
                }
                else
                    MessageBox.Show("Lỗi Update", "Update Lại", MessageBoxButtons.OKCancel);
            }
            catch
            {

            }

       
            
        }

         private void dgvdanhsach_SelectionChanged(object sender, EventArgs e)
        {
            mtxtMaNV.ReadOnly = true;
            mtxtMaNV.Text = this.dgvdanhsach.CurrentRow.Cells[0].Value.ToString();
            txtHoTen.Text = this.dgvdanhsach.CurrentRow.Cells[1].Value.ToString();
            dtpNgaySinh.Text = this.dgvdanhsach.CurrentRow.Cells[2].Value.ToString();
            txtDiaChi.Text = this.dgvdanhsach.CurrentRow.Cells[3].Value.ToString();
            txtsdt.Text = this.dgvdanhsach.CurrentRow.Cells[4].Value.ToString();
            txtEmail.Text = this.dgvdanhsach.CurrentRow.Cells[5].Value.ToString();
            cbbChuvu.Text = this.dgvdanhsach.CurrentRow.Cells[6].Value.ToString();
            cbbPhongban.Text = this.dgvdanhsach.CurrentRow.Cells[7].Value.ToString();
            if (cbbPhongban.Text == "Phòng tiếp nhận")
            {
                cbbPhongban.Enabled = false;
            }
            else
                cbbPhongban.Enabled = true;

        }

         private void button2_Click(object sender, EventArgs e)
         {
             mtxtMaNV.ReadOnly = false;
             mtxtMaNV.Text = "";
            txtHoTen.Text="";
            dtpNgaySinh.Value=DateTime.Now;
            txtDiaChi.Text="";
           txtsdt.Text="";
             txtEmail.Text="";
              cbbChuvu.Text="";
              cbbPhongban.Text = "";
         }

         private void button1_Click(object sender, EventArgs e)
         {
             LoadDanhSach();
             button2_Click(sender, e);
         }

         private void btnXoa_Click(object sender, EventArgs e)
         {
             string Ma = this.dgvdanhsach.CurrentRow.Cells[0].Value.ToString();
             nv_info.MaNV = Ma;
             int Kiemtra = nv_manager.NV_Delete(nv_info);
             try{
                 if (Kiemtra != 0)
                 {
                     MessageBox.Show("Bạn Có muốn xóa không?", "Delete", MessageBoxButtons.OKCancel);
                 }
                 else
                     MessageBox.Show("Lỗi Xóa");
             }
             catch
             {
             }

         }

         private void btnTim_Click(object sender, EventArgs e)
         {
             string tim = txtTim.Text;
             
             try
             {
                 if (rdMa.Checked == true)
                 {

                     dgvdanhsach.DataSource = nv_manager.NhanVien_TimTheoMaNV(tim);
                 }
                 else if (rdTen.Checked == true)
                 {
                     dgvdanhsach.DataSource = nv_manager.NhanVien_TimTheoTen(tim);

                 }
                 else if (rdChuVu.Checked == true)
                 {
                     dgvdanhsach.DataSource = nv_manager.NhanVien_TimTheoChuVu(tim);
                 }
                 else
                     dgvdanhsach.DataSource = nv_manager.NhanVien_TimTheoPhong(tim);
               
             }
             catch
             {
                 
             }
         }

         private void cbbChuvu_SelectedIndexChanged(object sender, EventArgs e)
         {
             if (cbbChuvu.Text == "Bác Sĩ")
             {
                 cbbPhongban.DataSource = this.nv_manager.PhongKham_All();
                 cbbPhongban.Enabled = true;
                 cbbPhongban.DisplayMember = "PhongKham";
                 mtxtMaNV.Mask = "BS.#####";
                // cbbPhongban.ValueMember = "PhongKham";
             }
             else
                 if (cbbChuvu.Text == "Y Tá")
                 {
                     cbbPhongban.Enabled = true;
                     cbbPhongban.DataSource = this.nv_manager.PhongDieuTri_All();
                     cbbPhongban.DisplayMember = "Tên Phòng";
                     // cbbPhongban.ValueMember = "PhongKham";
                     mtxtMaNV.Mask = "YT.#####";
                 }
                 else if (cbbChuvu.Text == "Nhân Viên Tiếp Nhận")
                 {
                     cbbPhongban.Text = "Phòng tiếp nhận";
                     cbbPhongban.Enabled = false;
                     mtxtMaNV.Mask = "NV.#####";
                 }
              
                
         }

         private void cbbPhongban_SelectedIndexChanged(object sender, EventArgs e)
         {

         }
    }
}
